<?php 

function sizeFilter($bytes)
{
// 	$label = array( 'B', 'KB', 'MB', 'GB', 'TB', 'PB' );
	$label = array('KB');
	for( $i = 0; $bytes >= 1024 && $i < ( count( $label ) -1 ); $bytes /= 1024, $i++ );
	return n(round( $bytes, 2 ));
}

?>
<script type="text/javascript">

$(document).ready(function() 
{
	
});
 
</script>

<div class="content">

	<div class="header">
		<div class="page-title">
                    <div>
                        <h1><i class="icon-user"></i> <?php echo h($box_title); ?></h1>
                        <h4><?php echo h($box_action); ?></h4>
                    </div>
        </div>		
	</div>

	<div class="container-fluid">
		<div class="row-fluid">

            <!-- BEGIN Main Content -->
			<div class="row-fluid">
                    <div class="span12">
                        <div class="box">
                            <div class="box-title">
                                <h3><i class="icon-user"></i> <?php echo h($box_action); ?></h3>
                            </div>
                            <div class="box-content">
                            
							<form method="post" class="form-horizontal" action="<?php echo site_url('home/config-system/backup-db'); ?>">
                                    <div class="row-fluid">
                                       <div class="span6">
                                          <!-- BEGIN Left Side -->
                                          <!-- END Left Side -->
                                       </div>
                                       <div class="span6 text-right">
                                          <!-- BEGIN Right Side -->
											<div class="control-group">
		                                      <label class="control-label"></label>
		                                      <div class="controls">
		                                      	<input type="submit" class="btn btn-primary" value="Backup Database To File" />
		                                        <select id="backup_type" name="backup_type" data-placeholder="Choose a Category" class="span3">
                                            		<option value="None">SQL</option>
                                            		<option value="Gzip">GZip</option>
                                         		</select>
                                                
                                            </div>
                                          <!-- END Right Side -->
                                       </div>
                                    </div>

                                 </form>                            
                            
                            
                                <table class="table table-striped table-hover fill-head">
                                    <thead>
                                        <tr>
                                            <th style="width: 50px">id</th>
                                            <th style="width: 150px">Filename</th>
                                            <th>Size (KB)</th>
                                            <th>Date & Time</th>
                                            <th>Type</th>
                                            <th></th>
                                            <th></th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                    
                                    <?php

                                    if ($dirhandle = opendir($upload_dir))
                                    {
                                    	$files=array();
                                    	while (false !== ($files[] = readdir($dirhandle)));
                                    	closedir($dirhandle);

                                    
                                        if (sizeof($files)>0)
									    { 
									      	rsort($files);
									      	$i = 1;
									      	foreach ($files as $dirfile)
									      	{ 
									        	if ($dirfile != "." && $dirfile != ".." && $dirfile!=basename($_SERVER["SCRIPT_FILENAME"]) && preg_match("/\.(sql|gz|csv)$/i",$dirfile)) :
									        	
									        	$type = "Misc";
									        	if (preg_match("/\.sql$/i",$dirfile))
									        		$type = "SQL";
									        	elseif (preg_match("/\.gz$/i",$dirfile))
									        		$type = "GZip";
									        	elseif (preg_match("/\.csv$/i",$dirfile))
									        		$type = "CSV";
									        	
									        	$import_link = "";
									        	$delete_link = "";
									        	if ((preg_match("/\.gz$/i",$dirfile) && function_exists("gzopen")) || preg_match("/\.sql$/i",$dirfile) || preg_match("/\.csv$/i",$dirfile))
									        	{
									        		$import_link = site_url('home/config-system/import-db/'.urlencode($dirfile));
									        		$import_link = "<a href='{$import_link}'>Import to database</a>";
									        		$delete_link = site_url('home/config-system/delete-db/'.urlencode($dirfile));
									        		$delete_link = "<a href='javascript:confimDelete(\"{$delete_link}\");'>Delete file</a>";
									        	}
									?>
												<tr>
													<td><?php echo $i; ?></td>
		                                            <td><?php echo $dirfile; ?></td>
		                                            <td><?php echo sizeFilter(filesize($upload_dir.'/'.$dirfile)); ?></td>
		                                            <td><?php echo date ("Y-m-d H:i:s", filemtime($upload_dir.'/'.$dirfile)); ?></td>
		                                            <td><?php echo $type; ?></td>
		                                            <td><?php echo $import_link; ?></td>
		                                            <td><?php echo $delete_link; ?></td>
												</tr>
									        	
									        	
									<?php 
												$i++;
									        	endif; 
									        }
									      }
									    }
                                    	

                                    
                                    
                                    
                                    ?>
                                    
                                        
                                        
                                        
                                    </tbody>
                                </table>
                                
                                
                            
                            </div>
                            
                        </div>
                    </div>
			</div>
			<!-- END Main Content -->
                			
			
		</div>
	</div>
</div>



